package whitelistcontroller

import (
	"context"
	"encoding/json"
	"vpn-management-server/logger"
	"vpn-management-server/network"
	"vpn-management-server/security/code"
	"vpn-management-server/src/dao/whitelistdao"
	"vpn-management-server/src/model/whitelistmodel"
	"vpn-management-server/src/service/adminservice"
	"vpn-management-server/src/service/ugroupservice"
	"vpn-management-server/src/service/whitelistservice"
)

func AddWhiteList(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if whiteList, ok := request.GetModel().(*Whitelistmodel.Whitelist); ok {
		admin := adminservice.GetCurrAdminService(ctx, request, response)
		if admin != nil {
			if ugroup := ugroupservice.GetUGroupByAdminNameService(admin.Name); ugroup != nil {
				whitelistservice.AddWhitelistService(ctx, ugroup.ID, whiteList.WhiteList, response)
				return
			} else {
				logger.Logger.ErrorfWithConn(ctx.Value("conn_id").(string), "admin %s not have user group\n", admin.Name)
				response.Code = code.BAD_REQUEST
				response.ErrorMsg = "admin not have user group"
			}
		} else {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get current admin fail")
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "get current admin fail"
		}
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to Whitelist fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to Whitelist fail"
	}
}

func DeleteWhiteList(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if whitelistIDs, ok := request.GetModel().(*Whitelistmodel.WhitelistIDs); ok {
		if id, err := whitelistdao.DeleteWhiteList(whitelistIDs.IDs); err != nil {
			logger.Logger.ErrorfWithConn(ctx.Value("conn_id").(string), "delete id %d fail", id)
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "delete whitelist fail"
			return
		}
		response.Code = code.SUCCESS
		logger.Logger.Infof("delete whitelist success")
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to whitelistIDs fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to whitelistIDs fail"
	}
}

func GetWhiteListByPage(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if page, ok := request.GetModel().(*Whitelistmodel.WhiteListPage); ok {
		var resData = struct {
			Whitelist []*Whitelistmodel.WhiteItem
			Count     int
		}{}
		whitelist := whitelistdao.GetWhiteListByPage(page.PageNo, page.PageSize, page.UGroupID)
		count := whitelistdao.GetWhiteListCountByUGroupID(page.UGroupID)
		if count == -1 {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get whitelist fail")
			response.Code = code.BAD_REQUEST
			response.ErrorMsg = "get whitelist info fail"
			return
		}
		if count == 0 {
			logger.Logger.Warn("white list is empty")
			whitelist = []*Whitelistmodel.WhiteItem{}
		}
		resData.Whitelist = whitelist
		resData.Count = count
		response.Code = code.SUCCESS
		data, _ := json.Marshal(resData)
		response.Data = data
		logger.Logger.Info("get whitelist success")
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to page fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to page fail"
	}
}
